package com.zzc.fwzpxt.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.zzc.fwzpxt.entity.Article;
import com.zzc.fwzpxt.entity.ArticleTag;
import com.zzc.fwzpxt.entity.Tag;
import com.zzc.fwzpxt.mapper.ArticleMapper;
import com.zzc.fwzpxt.mapper.ArticleTagMapper;
import com.zzc.fwzpxt.service.IArticleService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.zzc.fwzpxt.service.IArticleTagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 文章 article 服务实现类
 * </p>
 *
 * @author XiaoBingBy
 * @since 2017-09-26
 */
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements IArticleService {

    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private IArticleTagService iArticleTagService;

    @Transactional
    @Override
    public boolean addArticle(Article article, Long[] tags) {
        // 插入文章
        boolean articleResult = insert(article);
        // 插入文章标签 先删除 在插入
        if (articleResult) {
            ArrayList<ArticleTag> articleTags = new ArrayList<>();
            if (tags == null) {
                return true;
            }
            List<Long> list = Arrays.asList(tags);
            list.forEach(item -> {
                ArticleTag articleTag = new ArticleTag();
                articleTag.setArticleId(article.getId());
                articleTag.setArticleTagId(item);
                articleTags.add(articleTag);
            });
            //EntityWrapper<ArticleTag> wrapper = new EntityWrapper<>();
            //wrapper.eq("article_id", article.getId());
            //iArticleTagService.delete(wrapper);
            return iArticleTagService.insertBatch(articleTags) ? true : false;
        }
        return false;
    }

    @Override
    public boolean updateArticle(Article article, Long[] tags) {
        // 更新文章
        boolean articleResult = updateById(article);
        if (articleResult) {
            // 删除关联关系
            EntityWrapper<ArticleTag> wrapper = new EntityWrapper<>();
            wrapper.eq("article_id", article.getId());
            iArticleTagService.delete(wrapper);
            if (tags == null) {
                return true;
            }
            ArrayList<ArticleTag> articleTags = new ArrayList<>();
            List<Long> list = Arrays.asList(tags);
            list.forEach(item -> {
                ArticleTag articleTag = new ArticleTag();
                articleTag.setArticleId(article.getId());
                articleTag.setArticleTagId(item);
                articleTags.add(articleTag);
            });
            // 插入新的关联关系
            return iArticleTagService.insertBatch(articleTags) ? true : false;
        }
        return false;
    }


    @Override
    public Article findPrevArticle(Long id, Long type) {
        return articleMapper.findPrevArticle(id, type);
    }

    @Override
    public Article findNextArticle(Long id, Long type) {
        return articleMapper.findNextArticle(id, type);
    }
}
